package com.kiko.education.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.kiko.education.mapper.ClassInfoMapper;
import com.kiko.education.mapper.ClassMapper;
import com.kiko.education.mapper.CourseScoreMapper;
import com.kiko.education.model.Course;
import com.kiko.education.model.Score;
import com.kiko.education.service.CourseService;
import com.kiko.education.service.ScoreService;
import com.kiko.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;
    @Autowired
    private CourseScoreMapper courseScoreMapper;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ScoreService scoreService;

    private final String[] stringList1 = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    private final String[] stringList2 = {"（1，2）","（3，4）","（5，6）","（7，8）","（9，10）","（11，12）"};

    //学生选课跳转
    @RequestMapping(value="/stuSelectInfo", method = RequestMethod.GET)
    public String toSelectCourse(@RequestParam String userID, Model model){
        System.out.println(userID);
        List<Course> courseList = courseService.getAllCourse();
        List<String> teacherList = new ArrayList<>();
        for(int i=0;i<courseList.size();i++){
            String  teacher = userService.getByUserID(courseList.get(i).getCourseTeacher()).getUserName();
            teacherList.add(teacher);
        }
        model.addAttribute("userID", userID);
        model.addAttribute("teacherList", teacherList);
        model.addAttribute("courseList", courseList);
        return "stuSelect";
    }

    //选课结果
    @PostMapping("/processArray")
    public String processArray(@RequestBody Map<String, Object> jsonData) {
        String userID = jsonData.get("userID").toString();
        String list = jsonData.get("list").toString();
        String[] resultArray = list.split(",");
        // 在这里进行后端处理，例如打印到控制台
        for (String element : resultArray){
            String tid ="";
            tid = courseService.getCourseTeacherByID(Integer.parseInt(element));
            scoreService.setCourse(Integer.parseInt(element),tid,userID);
        }
        // 返回响应给客户端
        return "stuSelect";
    }

    //学生课程信息跳转
    @RequestMapping(value="/stuList", method = RequestMethod.GET)
    public String toCourseList(@RequestParam String userID, Model model){
        System.out.println(userID);
        List<Score> courseScoreList = scoreService.getCourseBySID(userID);
        List<String> teacherList = new ArrayList<>();
        List<Course> courseList = new ArrayList<>();
        for(int i=0;i<courseScoreList.size();i++){
            Course course = courseService.getCourseByID(courseScoreList.get(i).getCourseID());
            String  teacher = userService.getByUserID(courseScoreList.get(i).getTeacherID()).getUserName();
            courseList.add(course);
            teacherList.add(teacher);
        }
        model.addAttribute("userID", userID);
        model.addAttribute("teacherList", teacherList);
        model.addAttribute("courseList", courseList);
        return "stuList";
    }

    @RequestMapping(value="/studentInfo12", method = RequestMethod.POST)
    public String toDisplayCourse(String sid, Model model){
        List<Score> scoreList = scoreService.getCourseBySID(sid);
        List<String> nameList = new ArrayList<>();
        for(int i=0;i<scoreList.size();i++){
            nameList.add(courseService.getCourseNameByID(scoreList.get(i).getCourseID()));
        }
        model.addAttribute("nameList", nameList);
        model.addAttribute("scoreList", scoreList);
        return "studentInfo";
    }
}
